<?php 

//this script checks to see if the books are checked out and if not checks them out

//this is where we invoke the include.php file 
include 'connect.php';

//This gets info on the book
$barcode = $_GET['barcode'];
$book = mysql_query("SELECT * FROM books WHERE barcode = '$barcode'");
$book_get= mysql_fetch_array($book);

$name= $book_get['name'];
$patroncode = $_GET['patroncode'] ;
$checkout = $book_get['checkout'];

//Patron Ifomation
$patronsql = mysql_query("SELECT * FROM patrons WHERE code = '$patroncode'");
$patroninfo = mysql_fetch_array($patronsql);
$fpatronname = $patroninfo['fname'];
$lpatronname = $patroninfo['lname'];
$patronname = "$fpatronname";
//this checks to see if the book exists


$result = mysql_query("SELECT name FROM books WHERE barcode='$barcode'");
$num_rows = mysql_num_rows($result);



if ($num_rows == 0 ) { 
    header("Location: checkoutk.php?found=false&patron_coding=$patroncode&bar=". $barcode);
} 

if ($book_get['checkout'] == 'out')
{

  header("Location: checkoutk.php?found=out&bar=$barcode&patron_coding=$patroncode");
  exit;
  

}

$date = time();
//The Number of weeks 
//The Number of weeks out can be modified by changing the defualt 2 to your desired amount of weeks by the weeks after the + DO NOT MODIFY ANYTHING ELSE!

$checkout1sql = "UPDATE books SET outto = '$patroncode' WHERE barcode= $barcode";
$checkout2sql = "UPDATE books SET duedate = '$date' WHERE barcode= $barcode";
$checkout3sql = "UPDATE books SET checkout = 'out' WHERE barcode= $barcode";
//this inserts the data into the database
mysql_query($checkout1sql);
mysql_query($checkout2sql);
mysql_query($checkout3sql);
//This returns the user to the checkout page
header("Location: checkoutk.php?found=checked-out&bar=$barcode&bookname=$name&patron=$patronname&patron_coding=$patroncode");



?>

